package ldh.im.fxgui.controller.sub;

import com.google.common.eventbus.Subscribe;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import ldh.im.fxbase.data.event.EventBusFactory;
import ldh.im.fxbase.data.pojo.ImUser;
import ldh.im.fxbase.util.FxThreadUtil;
import ldh.im.fxbase.util.MovableHelper;
import ldh.im.fxbase.util.RegionUtil;
import ldh.im.fxgui.controller.BaseController;
import ldh.im.fxgui.controller.UserHeaderImageController;
import ldh.im.fxbase.util.ImCacheFactory;
import ldh.im.fxgui.util.UiUtil;
import ldh.im.fxgui.util.UserHeadImageUtil;

import java.net.URL;
import java.util.ResourceBundle;

/**
 *  显示用户图像
 */
public class UserHeaderIconController extends BaseController implements Initializable {

    @FXML private ImageView headImageView;
    @FXML private Label userNameLabel;
    @FXML private Label userIdLabel;

    private ImUser imUser;
    private volatile boolean isShow = false;

    public void setImUser(ImUser imUser) {
        this.imUser = imUser;
        headImageView.setImage(UserHeadImageUtil.getHeadImage(imUser.getHeadImg()));
        userNameLabel.setText(imUser.getUserName());
        userIdLabel.setText(imUser.getUserId());
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        EventBusFactory.getInstance().register(this);
        headImageView.setFitWidth(50);
        headImageView.setFitHeight(50);
        Rectangle clip = new Rectangle(headImageView.getFitWidth(), headImageView.getFitHeight());
        clip.setArcWidth(20);
        clip.setArcHeight(20);
        headImageView.setClip(clip);
    }

    public void changeIconEvent(MouseEvent mouseEvent) {
        if (isShow) return;
        Stage userIconStage = new Stage();
        userIconStage.initStyle(StageStyle.TRANSPARENT);
        RegionUtil.FxmlBean<Pane, UserHeaderImageController> fxmlBean = RegionUtil.loadFxmlBean(this.getClass().getResource("/fxml/UserHeaderImagePane.fxml"));
        fxmlBean.getController().setStage(userIconStage);
        fxmlBean.getController().setMainController(UiUtil.getMainController());
        Scene scene = new Scene(fxmlBean.getNode());
        scene.getStylesheets().add(this.getClass().getResource("/css/var.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/setting.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/stage.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/main.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/scrollbar.css").toExternalForm());
        scene.getStylesheets().add(this.getClass().getResource("/css/bootstrapfx.css").toExternalForm());
        userIconStage.setScene(scene);
        MovableHelper.movable(fxmlBean.getController().getMovablePane());
        userIconStage.show();
        userIconStage.setAlwaysOnTop(true);
        isShow = true;
        userIconStage.setOnCloseRequest(e->{
            isShow = false;
        });
        userIconStage.setOnHidden(e->{
            isShow = false;
        });
    }

    public void sendMessage(ActionEvent actionEvent) {
        imUser = ImCacheFactory.getInstance().getUser(imUser.getUserId());
        headImageView.setImage(UserHeadImageUtil.getHeadImage(imUser.getHeadImg()));
        if (mainController != null) {
            mainController.switchUserMessage(imUser, null);
            mainController.popupHide();
        }
    }

    /**
     * 用户信息发送变化，如：头像
     * @param imUser
     */
    @Subscribe
    public void userChange(ImUser imUser) {
        this.imUser = imUser;
        FxThreadUtil.runLater(()->{
            headImageView.setImage(UserHeadImageUtil.getHeadImage(imUser.getHeadImg()));
            userNameLabel.setText(imUser.getUserName());
            userIdLabel.setText(imUser.getUserId());
        });
    }
}
